package org.youyee.game.managers
{
	import org.youyee.game.event.MapObjectEvent;
	import org.youyee.game.object.MapObject;
	import org.youyee.game.pathFinding.PathMap;
	import org.youyee.game.pathFinding.PathNode;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.utils.Dictionary;

	/**
	 * 地图物体管理器
	 * @author youyee
	 * 
	 */	
	public class MapObjectManager
	{
		private var _itemLayer:DisplayObjectContainer;
		private var _mapObjects:Array;
		private var _numMapObjects:int;
		private var _pathMap:PathMap;
		
		/**
		 * 构造函数 
		 * @param itemLayer 物体所在的层
		 * @param pathMap 地图的逻辑对象
		 * 
		 */		
		public function MapObjectManager(itemLayer:DisplayObjectContainer, pathMap:PathMap)
		{
			_itemLayer = itemLayer;
			_mapObjects = [];
			_numMapObjects = 0;
			_pathMap = pathMap;
			
			_itemLayer.addEventListener(MapObjectEvent.OBJECT_DESTROY, handleItemDestroy);
		}
		
		/**
		 * 获取所有的物品的集合 
		 * @return 
		 * 
		 */		
		public function get mapObjects():Array
		{
			return this._mapObjects;
		}
		
		/**
		 * 添加一个物体到管理器当中 
		 * @param mapObject 需要被添加的物体
		 * @return 是否成功添加
		 * 
		 */		
		public function addMapObject(mapObject:MapObject):Boolean
		{
			if (!hasMapObject(mapObject))
			{
				this._mapObjects.push(mapObject);
				_numMapObjects = this._mapObjects.length;
				return true;
			}
			
			return false;
		}
		
		/**
		 * 需要被移除的物体 
		 * @param mapObject 需要被移除的物体
		 * @return 是否成功删除
		 * 
		 */		
		public function removeMapObject(mapObject:MapObject):Boolean
		{
			var index:int = _mapObjects.indexOf(mapObject);
			
			if (index != -1)
			{
				_mapObjects.splice(index, 1);
				_numMapObjects = _mapObjects.length;
			}
			
			return false;
		}
		
		/**
		 * 检测是否存在此物品 
		 * @param obj 需要被检测的物品
		 * @return 检测的结果
		 * 
		 */		
		public function hasMapObject(obj:MapObject):Boolean
		{
			if (this._mapObjects.indexOf(obj) != -1)
			{
				return true;
			}
			
			return false;
		}
		
		/**
		 * 检测物品的位置是否和其他的物品在地图上产生了重叠 
		 * @param mapObject 接受检测的物品
		 * @return 重叠的位置节点
		 * 
		 */		
		public function checkMapObjectPosition(mapObject:MapObject):Array
		{
			var len:int = this._mapObjects.length;
			var result:Array = [];
			
			for (var i:int=0; i<len; ++i)
			{
				if (this._mapObjects[i] != mapObject)
				{
					result = result.concat(checkPosition(mapObject, this._mapObjects[i] as MapObject));
				}
			}
			
			return result;
		}
		
		private function checkPosition(a:MapObject, b:MapObject):Array
		{
			var result:Array = [];
			
			var aNodes:Array = getObjectPossessNodes(a);
			var bNodes:Array = getObjectPossessNodes(b);
			
			var len:int = aNodes.length;
			var index:int = -1;
			
			var p:PathNode;
			
			for (var i:int=0; i<len; ++i)
			{
				p = aNodes[i] as PathNode;
				index = bNodes.indexOf(p);
				
				if (index != -1)
				{
					result.push(p);
				}
			}
			
			return result;
		}
		
		/**
		 * 获取一个物体所占的  
		 * @param obj 物品所占的节点
		 * @return 节点的集合
		 * 
		 */		
		public function getObjectPossessNodes(obj:MapObject):Array
		{
			var result:Array = [];
			
			for (var i:int=0; i<obj.itemWidth; ++i)
			{
				for (var j:int=0; j<obj.itemHeight; ++j)
				{
					result.push(this._pathMap.getPathNodeByPosition(i + obj.pathNode.x, j + obj.pathNode.y));
				}
			}
			
			return result;
		}
		
		private function handleItemDestroy(e:MapObjectEvent):void
		{
			var item:MapObject = e.target as MapObject;
			
			if (item)
			{
				removeMapObject(item);
			}
		}
		
		/**
		 * 销毁管理器 
		 * 
		 */		
		public function destroy():void
		{
			if (!_itemLayer)
			{
				return ;
			}
			
			_itemLayer.removeEventListener(MapObjectEvent.OBJECT_DESTROY, handleItemDestroy);
			
			_mapObjects = null;
			_numMapObjects = 0;
			_pathMap = null;
			_itemLayer = null;
		}
	}
}